@extends('layouts.buz.layout', ['header_active' => 'member'])

@section('sidebar')
    @include('layouts.buz.sidebar.member', ['active' => 'pwd'])
@endsection()
@section('title')
    个人中心 - 密码修改
@endsection()
@section('content')
    {{--密码管理--}}
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title">
                密码管理
                <button type="button" class="btn btn-default right" data-toggle="modal" data-target="#myModal">修改</button>
            </h4>
        </div>
        <div class="panel-body">
            <form class="form-horizontal member bottom_line" role="form" id="updataLoginP">
                <div class="form-group">
                    <label for="lastname" class="col-xs-4 col-sm-2 control-label">原密码：</label>
                    <div class="col-xs-10 col-sm-5 col-md-4 col-lg-4">
                        <input type="password" class="form-control" name="login_pwd">
                    </div>
                </div>
                <div class="form-group">
                    <label for="lastname" class="col-xs-4 col-sm-2 control-label">新密码：</label>
                    <div class="col-xs-10 col-sm-5 col-md-4 col-lg-4">
                        <input type="password" class="form-control" name="new_login_pwd">
                    </div>
                </div>
                <div class="form-group">
                    <label for="lastname" class="col-xs-4 col-sm-2 control-label">确认密码：</label>
                    <div class="col-xs-10 col-sm-5 col-md-4 col-lg-4">
                        <input type="password" class="form-control" name="new_login_pwd_confirmation">
                    </div>
                </div>
                <div class="form-group">
                    <label for="lastname" class="col-xs-4 col-sm-2 control-label">验证码：</label>
                    <div class="col-xs-6 col-sm-3 col-md-2 col-lg-2">
                        <input type="text" class="form-control" name="sms_code">
                    </div>
                    <div class="col-xs-6 col-sm-5 col-md-4 col-lg-4">
                        <button type="button" class="btn btn-info changePwd" data-type="login" id="SendCode">发送手机验证码</button>
                    </div>
                </div>
                <div class="form-group">
                    <label for="firstname" class="col-sm-2 control-label"></label>
                    <div class="col-xs-10 col-sm-5 col-md-4 col-lg-4">
                        <button type="button" class="btn btn-primary" id="submitLogin">确定</button>
                    </div>
                </div>
            </form>
            <form class="form-horizontal member" role="form" id="updataTradeP">
                <div class="form-group">
                    <label for="lastname" class="col-xs-4 col-sm-2 control-label">原交易密码：</label>
                    <div class="col-xs-10 col-sm-5 col-md-4 col-lg-4">
                        <input type="password" class="form-control" name="trade_pwd">
                    </div>
                </div>
                <div class="form-group">
                    <label for="lastname" class="col-xs-4 col-sm-2 control-label">新交易密码：</label>
                    <div class="col-xs-10 col-sm-5 col-md-4 col-lg-4">
                        <input type="password" class="form-control" name="new_trade_pwd">
                    </div>
                </div>
                <div class="form-group">
                    <label for="lastname" class="col-xs-4 col-sm-2 control-label">确认交易密码：</label>
                    <div class="col-xs-10 col-sm-5 col-md-4 col-lg-4">
                        <input type="password" class="form-control" name="new_trade_pwd_confirmation">
                    </div>
                </div>
                <div class="form-group">
                    <label for="lastname" class="col-xs-4 col-sm-2 textRight">验证码：</label>
                    <div class="col-xs-6 col-sm-3 col-md-2 col-lg-2">
                        <input type="text" class="form-control" name="sms_code">
                    </div>
                    <div class="col-xs-6 col-sm-5 col-md-4 col-lg-4">
                        <button type="button" class="btn btn-info changePwd" data-type="trade" id="SendCode">发送手机验证码</button>
                    </div>
                </div>
                <div class="form-group">
                    <label for="firstname" class="col-sm-2 control-label"></label>
                    <div class="col-xs-10 col-sm-5 col-md-4 col-lg-4">
                        <button type="button" class="btn btn-primary" id="submitTrade">确定</button>
                        {{--<a href="" style="text-decoration: underline;">忘记密码</a>--}}
                    </div>
                </div>
            </form>
        </div>
    </div>
    @endsection()
@section('bottom_js')
    <script>
        $('#updataLoginP').bootstrapValidator({
            message: '这个值没有被验证',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                login_pwd: {
                    message: '密码还没有验证',
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        }
                    }
                },
                new_login_pwd: {
                    message: '密码还没有验证',
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        },
                        stringLength: {
                            min: 6,
                            max: 20,
                            message: '密码只能由6-20位字母、数字和下划线组成'
                        },
                        regexp: {
                            regexp: /^[0-9a-zA-Z_]{1,}$/,
                            message: '密码只能由6-20位字母、数字和下划线组成'
                        }
                    }
                },
                new_login_pwd_confirmation: {
                    message: '密码重复还没有验证',
                    validators: {
                        notEmpty: {
                            message: '密码重复不能为空'
                        },
                        stringLength: {
                            min: 6,
                            max: 16,
                            message: '密码长度在6到16之间'
                        },
                        identical: {
                            field: 'new_login_pwd',
                            message: '两次密码不同请重新输入'
                        }
                    }
                },
                sms_code: {
                    message: '验证码不能为空',
                    validators: {
                        notEmpty: {
                            message: '验证码不能为空'
                        }
                    }
                },
            }
        });
        $('#updataTradeP').bootstrapValidator({
            message: '这个值没有被验证',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                trade_pwd: {
                    message: '密码还没有验证',
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        }
                    }
                },
                new_trade_pwd: {
                    message: '密码还没有验证',
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        },
                        stringLength: {
                            min: 6,
                            max: 20,
                            message: '密码只能由6-20位字母、数字和下划线组成'
                        },
                        regexp: {
                            regexp: /^[0-9a-zA-Z_]{1,}$/,
                            message: '密码只能由6-20位字母、数字和下划线组成'
                        }
                    }
                },
                new_trade_pwd_confirmation: {
                    message: '密码重复还没有验证',
                    validators: {
                        notEmpty: {
                            message: '密码重复不能为空'
                        },
                        stringLength: {
                            min: 6,
                            max: 16,
                            message: '密码长度在6到16之间'
                        },
                        identical: {
                            field: 'new_trade_pwd',
                            message: '两次密码不同请重新输入'
                        }
                    }
                },
                sms_code: {
                    message: '验证码不能为空',
                    validators: {
                        notEmpty: {
                            message: '验证码不能为空'
                        }
                    }
                },
            }
        });
        $(".changePwd").click(function(){
            var that = $(this);
            var type = that.attr('data-type');
            type = type == 'trade' ? 7 : 8;
            $.ajax({
                url: "/api/tool/sms",
                data: {type: type},
                type: 'post',
                success:function(data){
                    if(data.code == 0){
                        setTime(that);
                        showMsg("发送成功");
                    }else {
                        showMsg(data.message);
                    }
                }
            });
        });
        $(function () {
            //修改登录密码
            $("#submitLogin").click(function () {
                var bootstrapValidator = $('#updataLoginP').data("bootstrapValidator");
                bootstrapValidator.validate();
                if(bootstrapValidator.isValid()){
                    var data = $("#updataLoginP").serialize();
                    console.log(data);
                    $.ajax({
                        url: "/api/profile/pwd/login",
                        data: data,
                        type: 'post',
                        success:function(data){
                            console.log(data);
                            if(filterAjaxData(data)){
                                showMsg("修改成功, 请重新登录");
                                localStorage.setItem('token', null);
                                window.location.href="/login";
                            }

                        }

                    });
                }
            });
            //修改交易密码
            $("#submitTrade").click(function () {
                var bootstrapValidator = $('#updataTradeP').data("bootstrapValidator");
                bootstrapValidator.validate();
                if(bootstrapValidator.isValid()){
                    var data = $("#updataTradeP").serialize();
                    console.log(data);
                    $.ajax({
                        url: "/api/profile/pwd/trade",
                        data: data,
                        type: 'post',
                        success:function(data){
                            console.log(data);
                            if(filterAjaxData(data)){
                                showMsg("修改成功");
                                location=location;
//                                window.location.href="/login";
//                            }else {
//                                alert(data.message);
                            }
                        }
                    });
                }
            })
        })

    </script>
@endsection()
